package kyluzoi.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Kyluzoi
 * Create Date: 2016/11/28
 * Email:Kyluzoi@gmail.com
 * Path转动画基类
 */

public class PathBaseView extends View {

    protected Path mBasePath;   //需要做动画的源Path
    protected Path mAnimPath;   //用于绘制动画的Path


    protected Paint mPaint;
    protected int mColorBg = Color.GRAY;    //背景色
    protected int mColorFg = Color.WHITE;   //前景色 填充色

    protected int mPaddingLeft, mPaddingTop;
    protected PathAnimationHelper mPathAnimationHelper;

    public PathBaseView(Context context) {
        this(context, null);
    }

    public PathBaseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathBaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        //Paint初始化操作
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(3);

        mAnimPath = new Path(); //动画路径初始化操作

        initAnimHelper();


    }

    /**
     * 初始化动画helper类
     */
    private void initAnimHelper() {
        mPathAnimationHelper = getInitAnimHeper();
    }

    /**
     * 子类可通过重写这个方法，返回自定义的AnimHelper
     *
     * @return
     */
    protected PathAnimationHelper getInitAnimHeper() {
        //长度问题解决
        return new PathAnimationHelper(this, mBasePath, mAnimPath);
    }


    /**
     * view的绘制方法
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mPaddingLeft, mPaddingTop);        //平移

        try {

            //先绘制底，以及边框
            mPaint.setColor(mColorBg);
            canvas.drawPath(mBasePath, mPaint);

            //再绘制前景，mAnimPath不断变化，不断重绘View的话，就会有动画效果。
            mPaint.setColor(mColorFg);
            canvas.drawPath(mAnimPath, mPaint);
        } catch (NullPointerException e) {
            Log.d("PathBaseView", "pathBaseView 初始化时，有某些参数没传，防止崩溃，抛出异常");
        }
    }

    /**
     * view Size变化方法
     *
     * @param w    变化后的宽
     * @param h    变化后的高
     * @param oldw 变化前的宽
     * @param oldh 变化前的高
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //防止view padding失效
        mPaddingLeft = getPaddingLeft();
        mPaddingTop = getPaddingTop();
    }


    /**
     * 清除并停止动画
     */
    public void clearAnim() {
        stopAnim();
        mAnimPath.reset();
        mAnimPath.lineTo(0, 0);
        invalidate();
    }


    /**
     * 执行循环动画
     */
    public void startAnim() {
        mPathAnimationHelper.startAnim();
    }

    /**
     * 动画停止
     */
    private void stopAnim() {
        mPathAnimationHelper.stopAnim();
    }


    /**
     * 设置动画 循环
     */
    public PathBaseView setAnimInfinite(boolean infinite) {
        mPathAnimationHelper.setInfinite(infinite);
        return this;
    }

    /**
     * 设置动画 总时长
     */
    public PathBaseView setAnimTime(long animTime) {
        mPathAnimationHelper.setAnimTime(animTime);
        return this;
    }


    public PathAnimationHelper getPathAnimHelper() {
        return mPathAnimationHelper;
    }

    public PathBaseView setPathAnimHelper(PathAnimationHelper pathAnimHelper) {
        mPathAnimationHelper = pathAnimHelper;
        return this;
    }

    public Path getAnimPath() {
        return mAnimPath;
    }

    public Path getBasePath() {
        return mBasePath;
    }

    public PathBaseView setBasePath(Path basePath) {
        mBasePath = basePath;
        //一般 basepath与animationpath相同
        mAnimPath = basePath; // 默认相同
        initAnimHelper();
        return this;
    }

    public PathBaseView setAnimPath(Path animPath) {
        mAnimPath = animPath;
        return this;
    }

    public Paint getPaint() {
        return mPaint;
    }

    public PathBaseView setPaint(Paint paint) {
        mPaint = paint;
        return this;
    }

    public int getColorBg() {
        return mColorBg;
    }

    public PathBaseView setColorBg(int colorBg) {
        mColorBg = colorBg;
        return this;
    }

    public int getColorFg() {
        return mColorFg;
    }

    public PathBaseView setColorFg(int colorFg) {
        mColorFg = colorFg;
        return this;
    }

}
